package com.xjgzinfo.usdp.web.rest;

import com.xjgzinfo.usdp.AvueServer4jApp;
import com.xjgzinfo.usdp.config.TestSecurityConfiguration;
import com.xjgzinfo.usdp.domain.BladeVisualMap;
import com.xjgzinfo.usdp.repository.BladeVisualMapRepository;
import com.xjgzinfo.usdp.service.BladeVisualMapService;
import com.xjgzinfo.usdp.service.dto.BladeVisualMapDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualMapMapper;
import com.xjgzinfo.usdp.service.dto.BladeVisualMapCriteria;
import com.xjgzinfo.usdp.service.BladeVisualMapQueryService;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Base64Utils;
import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Integration tests for the {@link BladeVisualMapResource} REST controller.
 */
@SpringBootTest(classes = { AvueServer4jApp.class, TestSecurityConfiguration.class })
@AutoConfigureMockMvc
@WithMockUser
public class BladeVisualMapResourceIT {

    private static final String DEFAULT_NAME = "AAAAAAAAAA";
    private static final String UPDATED_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_DATA = "AAAAAAAAAA";
    private static final String UPDATED_DATA = "BBBBBBBBBB";

    @Autowired
    private BladeVisualMapRepository bladeVisualMapRepository;

    @Autowired
    private BladeVisualMapMapper bladeVisualMapMapper;

    @Autowired
    private BladeVisualMapService bladeVisualMapService;

    @Autowired
    private BladeVisualMapQueryService bladeVisualMapQueryService;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restBladeVisualMapMockMvc;

    private BladeVisualMap bladeVisualMap;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static BladeVisualMap createEntity(EntityManager em) {
        BladeVisualMap bladeVisualMap = new BladeVisualMap()
            .name(DEFAULT_NAME)
            .data(DEFAULT_DATA);
        return bladeVisualMap;
    }
    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static BladeVisualMap createUpdatedEntity(EntityManager em) {
        BladeVisualMap bladeVisualMap = new BladeVisualMap()
            .name(UPDATED_NAME)
            .data(UPDATED_DATA);
        return bladeVisualMap;
    }

    @BeforeEach
    public void initTest() {
        bladeVisualMap = createEntity(em);
    }

    @Test
    @Transactional
    public void createBladeVisualMap() throws Exception {
        int databaseSizeBeforeCreate = bladeVisualMapRepository.findAll().size();
        // Create the BladeVisualMap
        BladeVisualMapDTO bladeVisualMapDTO = bladeVisualMapMapper.toDto(bladeVisualMap);
        restBladeVisualMapMockMvc.perform(post("/api/blade-visual-maps").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(bladeVisualMapDTO)))
            .andExpect(status().isCreated());

        // Validate the BladeVisualMap in the database
        List<BladeVisualMap> bladeVisualMapList = bladeVisualMapRepository.findAll();
        assertThat(bladeVisualMapList).hasSize(databaseSizeBeforeCreate + 1);
        BladeVisualMap testBladeVisualMap = bladeVisualMapList.get(bladeVisualMapList.size() - 1);
        assertThat(testBladeVisualMap.getName()).isEqualTo(DEFAULT_NAME);
        assertThat(testBladeVisualMap.getData()).isEqualTo(DEFAULT_DATA);
    }

    @Test
    @Transactional
    public void createBladeVisualMapWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = bladeVisualMapRepository.findAll().size();

        // Create the BladeVisualMap with an existing ID
        bladeVisualMap.setId(1L);
        BladeVisualMapDTO bladeVisualMapDTO = bladeVisualMapMapper.toDto(bladeVisualMap);

        // An entity with an existing ID cannot be created, so this API call must fail
        restBladeVisualMapMockMvc.perform(post("/api/blade-visual-maps").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(bladeVisualMapDTO)))
            .andExpect(status().isBadRequest());

        // Validate the BladeVisualMap in the database
        List<BladeVisualMap> bladeVisualMapList = bladeVisualMapRepository.findAll();
        assertThat(bladeVisualMapList).hasSize(databaseSizeBeforeCreate);
    }


    @Test
    @Transactional
    public void getAllBladeVisualMaps() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(bladeVisualMap.getId().intValue())))
            .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
            .andExpect(jsonPath("$.[*].data").value(hasItem(DEFAULT_DATA.toString())));
    }
    
    @Test
    @Transactional
    public void getBladeVisualMap() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get the bladeVisualMap
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps/{id}", bladeVisualMap.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(bladeVisualMap.getId().intValue()))
            .andExpect(jsonPath("$.name").value(DEFAULT_NAME))
            .andExpect(jsonPath("$.data").value(DEFAULT_DATA.toString()));
    }


    @Test
    @Transactional
    public void getBladeVisualMapsByIdFiltering() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        Long id = bladeVisualMap.getId();

        defaultBladeVisualMapShouldBeFound("id.equals=" + id);
        defaultBladeVisualMapShouldNotBeFound("id.notEquals=" + id);

        defaultBladeVisualMapShouldBeFound("id.greaterThanOrEqual=" + id);
        defaultBladeVisualMapShouldNotBeFound("id.greaterThan=" + id);

        defaultBladeVisualMapShouldBeFound("id.lessThanOrEqual=" + id);
        defaultBladeVisualMapShouldNotBeFound("id.lessThan=" + id);
    }


    @Test
    @Transactional
    public void getAllBladeVisualMapsByNameIsEqualToSomething() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList where name equals to DEFAULT_NAME
        defaultBladeVisualMapShouldBeFound("name.equals=" + DEFAULT_NAME);

        // Get all the bladeVisualMapList where name equals to UPDATED_NAME
        defaultBladeVisualMapShouldNotBeFound("name.equals=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    public void getAllBladeVisualMapsByNameIsNotEqualToSomething() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList where name not equals to DEFAULT_NAME
        defaultBladeVisualMapShouldNotBeFound("name.notEquals=" + DEFAULT_NAME);

        // Get all the bladeVisualMapList where name not equals to UPDATED_NAME
        defaultBladeVisualMapShouldBeFound("name.notEquals=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    public void getAllBladeVisualMapsByNameIsInShouldWork() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList where name in DEFAULT_NAME or UPDATED_NAME
        defaultBladeVisualMapShouldBeFound("name.in=" + DEFAULT_NAME + "," + UPDATED_NAME);

        // Get all the bladeVisualMapList where name equals to UPDATED_NAME
        defaultBladeVisualMapShouldNotBeFound("name.in=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    public void getAllBladeVisualMapsByNameIsNullOrNotNull() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList where name is not null
        defaultBladeVisualMapShouldBeFound("name.specified=true");

        // Get all the bladeVisualMapList where name is null
        defaultBladeVisualMapShouldNotBeFound("name.specified=false");
    }
                @Test
    @Transactional
    public void getAllBladeVisualMapsByNameContainsSomething() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList where name contains DEFAULT_NAME
        defaultBladeVisualMapShouldBeFound("name.contains=" + DEFAULT_NAME);

        // Get all the bladeVisualMapList where name contains UPDATED_NAME
        defaultBladeVisualMapShouldNotBeFound("name.contains=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    public void getAllBladeVisualMapsByNameNotContainsSomething() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        // Get all the bladeVisualMapList where name does not contain DEFAULT_NAME
        defaultBladeVisualMapShouldNotBeFound("name.doesNotContain=" + DEFAULT_NAME);

        // Get all the bladeVisualMapList where name does not contain UPDATED_NAME
        defaultBladeVisualMapShouldBeFound("name.doesNotContain=" + UPDATED_NAME);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultBladeVisualMapShouldBeFound(String filter) throws Exception {
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(bladeVisualMap.getId().intValue())))
            .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
            .andExpect(jsonPath("$.[*].data").value(hasItem(DEFAULT_DATA.toString())));

        // Check, that the count call also returns 1
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(content().string("1"));
    }

    /**
     * Executes the search, and checks that the default entity is not returned.
     */
    private void defaultBladeVisualMapShouldNotBeFound(String filter) throws Exception {
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$").isArray())
            .andExpect(jsonPath("$").isEmpty());

        // Check, that the count call also returns 0
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(content().string("0"));
    }

    @Test
    @Transactional
    public void getNonExistingBladeVisualMap() throws Exception {
        // Get the bladeVisualMap
        restBladeVisualMapMockMvc.perform(get("/api/blade-visual-maps/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateBladeVisualMap() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        int databaseSizeBeforeUpdate = bladeVisualMapRepository.findAll().size();

        // Update the bladeVisualMap
        BladeVisualMap updatedBladeVisualMap = bladeVisualMapRepository.findById(bladeVisualMap.getId()).get();
        // Disconnect from session so that the updates on updatedBladeVisualMap are not directly saved in db
        em.detach(updatedBladeVisualMap);
        updatedBladeVisualMap
            .name(UPDATED_NAME)
            .data(UPDATED_DATA);
        BladeVisualMapDTO bladeVisualMapDTO = bladeVisualMapMapper.toDto(updatedBladeVisualMap);

        restBladeVisualMapMockMvc.perform(put("/api/blade-visual-maps").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(bladeVisualMapDTO)))
            .andExpect(status().isOk());

        // Validate the BladeVisualMap in the database
        List<BladeVisualMap> bladeVisualMapList = bladeVisualMapRepository.findAll();
        assertThat(bladeVisualMapList).hasSize(databaseSizeBeforeUpdate);
        BladeVisualMap testBladeVisualMap = bladeVisualMapList.get(bladeVisualMapList.size() - 1);
        assertThat(testBladeVisualMap.getName()).isEqualTo(UPDATED_NAME);
        assertThat(testBladeVisualMap.getData()).isEqualTo(UPDATED_DATA);
    }

    @Test
    @Transactional
    public void updateNonExistingBladeVisualMap() throws Exception {
        int databaseSizeBeforeUpdate = bladeVisualMapRepository.findAll().size();

        // Create the BladeVisualMap
        BladeVisualMapDTO bladeVisualMapDTO = bladeVisualMapMapper.toDto(bladeVisualMap);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restBladeVisualMapMockMvc.perform(put("/api/blade-visual-maps").with(csrf())
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(bladeVisualMapDTO)))
            .andExpect(status().isBadRequest());

        // Validate the BladeVisualMap in the database
        List<BladeVisualMap> bladeVisualMapList = bladeVisualMapRepository.findAll();
        assertThat(bladeVisualMapList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    public void deleteBladeVisualMap() throws Exception {
        // Initialize the database
        bladeVisualMapRepository.saveAndFlush(bladeVisualMap);

        int databaseSizeBeforeDelete = bladeVisualMapRepository.findAll().size();

        // Delete the bladeVisualMap
        restBladeVisualMapMockMvc.perform(delete("/api/blade-visual-maps/{id}", bladeVisualMap.getId()).with(csrf())
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<BladeVisualMap> bladeVisualMapList = bladeVisualMapRepository.findAll();
        assertThat(bladeVisualMapList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
